// Login box
$(document).ready(function() {

    $(".signin").click(function(e) {
        e.preventDefault();
        $("fieldset#signin_menu").toggle();
        $(".signin").toggleClass("menu-open");
    });

    $("fieldset#signin_menu").mouseup(function() {
        return false
    });
    $(document).mouseup(function(e) {
        if($(e.target).parent("a.signin").length==0) {
            $(".signin").removeClass("menu-open");
            $("fieldset#signin_menu").hide();
        }
    });

});
//Tip script
$(document).ready(function() {
	//Tooltips
	$(".tip_trigger").hover(function(){
		tip = $(this).find('.tip');
		tip.show(); //Show tooltip
	}, function() {
		tip.hide(); //Hide tooltip
	}).mousemove(function(e) {
		var mousex = e.pageX + 20; //Get X coodrinates
		var mousey = e.pageY + 20; //Get Y coordinates
		var tipWidth = tip.width(); //Find width of tooltip
		var tipHeight = tip.height(); //Find height of tooltip

		//Distance of element from the right edge of viewport
		var tipVisX = $(window).width() - (mousex + tipWidth);
		//Distance of element from the bottom of viewport
		var tipVisY = $(window).height() - (mousey + tipHeight);

		if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
			mousex = e.pageX - tipWidth - 20;
		}if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
			mousey = e.pageY - tipHeight - 20;
		}
		tip.css({top: mousey, left: mousex});
	});
});

//register.tpl
$(document).ready(function()
{

$('#company_radio').click(function()
{
        $('#student_block').hide();
        $('#company_block').show();
});

$('#student_radio').click(function()
{
        $('#company_block').hide();
        $('#student_block').show();
});


$("#s_faculty").change(function()
{
    var id=$(this).val();
    var dataString = 'id='+id;

    $.ajax
    ({
        type: "POST",
        url: "functions/getSpecialization.php",
        data: dataString,
        cache: false,
        success: function(html)
        {
            $("#s_specialization").html(html);
        }
});
});
});
//Table sort
$(document).ready(function()
    {
        $("table").tablesorter( {sortList: [[0,0], [1,0]]} );
    }
);
 //Validate
$(function() {
    $('#register').validity(function() {
        $("#register #email").require();
        $("#register #password").require();
        if($("#register input[type='radio']:checked").val() == 'student'){
            
            $("#s_name").require();
            $("#s_surname").require();
            $("#s_album").require().match("integer").range(11111,99999);
            $("#s_faculty")
                .greaterThan(0, "Wybierz jedną z opcji.");
        }else{
            $("#c_name").require();
            $("#c_person_name").require();
            $("#c_person_surname").require();
            $("#c_person_email").require();
            $("#c_person_phone").require();
            $("#c_person_position").require();
        }
    });
    $('#new_practice').validity(function(){
        $("#date_start")
            .require()
            .match("date")
            .greaterThanOrEqualTo(new Date(),"Data nie może być wcześniejsza od dzisiejszej");
        $("#date_end")
            .require()
            .match("date");
        $("#amount").require().match("integer");
    });
});
$(document).ready(function()
{
    $("#register #email").change(function()
    {
        var email = $("#register #email").val();
        var msgbox = $("#register #status_email");

        if(email.length > 3)
        {
            $("#register #status_email").html('<img src="images/loader.gif">&nbsp;Sprawdzam...');

            $.ajax({
                type: "POST",
                url: "functions/checkEmail.php",
                data: "email="+ email,
                success: function(msg){
                    $("#status_email").ajaxComplete(function(event, request){

                    if(msg == 'OK'){
                        $("#register #email").removeClass("red");
                        $("#register #email").addClass("green");
                        msgbox.html('<img src="images/tick.png"> <font color="Green"> OK </font>');
                    }
                    else{
                        $("#register #email").removeClass("green");
                        $("#register #email").addClass("red");
                        msgbox.html(msg);
                    }
                });
            }
            });
        }
        else{
            $("#register #email").addClass("red");
            $("#register #status_email").html('<font color="#cc0000">Wprowadź poprawny adres mail</font>');
        }
    return false;
    });
});
//Select all checkboxes
function jqCheckAll( id, name, flag )
{
   if (flag == 0)
   {
      $("form#" + id + " INPUT[@name=" + name + "][type='checkbox']").attr('checked', false);
   }
   else
   {
      $("form#" + id + " INPUT[@name=" + name + "][type='checkbox']").attr('checked', true);
   }
}
//Fancybox
$(document).ready(function(){
	$(".extLink").fancybox({
		 'width' : '90%',
		 'height' : '85%',
		 'autoScale' : false,
		 'transitionIn' : 'none',
		 'transitionOut' : 'none',
		 'type' : 'iframe'
	 });
        $("a#inline").fancybox({
	});

});

//TABS
$(document).ready(function() {

	//When page loads...
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs li").click(function() {

		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});

});